/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class BusinessProfitListExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"BusinessProfitList.xls\"");
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints(10);
		font.setColor(10);
		font.setFontName("黑体");
		font.setBoldweight(700);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFont(font);
		cellStyle.setAlignment(2);
		cellStyle.setBorderBottom(1);
		cellStyle.setWrapText(true);
		cellStyle.setFillForegroundColor(22);
		cellStyle.setFillPattern(1);
		HSSFSheet sheet = workbook.createSheet("list");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		r.createCell(11);
		r.createCell(12);
		r.createCell(13);
		r.createCell(14);
		r.createCell(15);
		r.createCell(16);
		r.createCell(17);
		r.createCell(18);
		c.setCellValue(new HSSFRichTextString("毛利小计：" + model.get("profitCost")
				+ "元 毛利率：" + model.get("profitPercent") + "%"));
		sheet.addMergedRegion(new Region(0, 0, 0, 18));
		r = sheet.createRow(1);
		r.createCell(0).setCellValue(new HSSFRichTextString("客户"));
		r.createCell(1).setCellValue(new HSSFRichTextString("订货日期"));
		r.createCell(2).setCellValue(new HSSFRichTextString("单号"));
		r.createCell(3).setCellValue(new HSSFRichTextString("摘要"));
		r.createCell(4).setCellValue(new HSSFRichTextString("收入"));
		r.createCell(5);
		r.createCell(6).setCellValue(new HSSFRichTextString("成本"));
		r.createCell(7);
		r.createCell(8);
		r.createCell(9).setCellValue(new HSSFRichTextString("毛利"));
		r.createCell(10).setCellValue(new HSSFRichTextString("毛利率"));
		r.createCell(11).setCellValue(new HSSFRichTextString("已收金额"));
		r.createCell(12).setCellValue(new HSSFRichTextString("尚欠金额"));
		r.createCell(13).setCellValue(new HSSFRichTextString("业务员"));
		r.createCell(14).setCellValue(new HSSFRichTextString("设计员"));
		r.createCell(15).setCellValue(new HSSFRichTextString("跟单员"));
		r.createCell(16).setCellValue(new HSSFRichTextString("营销来源"));
		r.createCell(17).setCellValue(new HSSFRichTextString("开单人"));
		r.createCell(18).setCellValue(new HSSFRichTextString("结清日期"));
		r = sheet.createRow(2);
		r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4).setCellValue(new HSSFRichTextString("合同金额"));
		r.createCell(5).setCellValue(new HSSFRichTextString("已调账"));
		r.createCell(6).setCellValue(new HSSFRichTextString("材料"));
		r.createCell(7).setCellValue(new HSSFRichTextString("外协"));
		r.createCell(8).setCellValue(new HSSFRichTextString("费用"));
		r.createCell(9);
		r.createCell(10);
		r.createCell(11);
		r.createCell(12);
		r.createCell(13);
		r.createCell(14);
		r.createCell(15);
		r.createCell(16);
		r.createCell(17);
		r.createCell(18);
		sheet.addMergedRegion(new Region(1, 0, 2, 0));
		sheet.addMergedRegion(new Region(1, 1, 2, 1));
		sheet.addMergedRegion(new Region(1, 2, 2, 2));
		sheet.addMergedRegion(new Region(1, 3, 2, 3));
		sheet.addMergedRegion(new Region(1, 4, 1, 5));
		sheet.addMergedRegion(new Region(1, 6, 1, 8));
		sheet.addMergedRegion(new Region(1, 9, 2, 9));
		sheet.addMergedRegion(new Region(1, 10, 2, 10));
		sheet.addMergedRegion(new Region(1, 11, 2, 11));
		sheet.addMergedRegion(new Region(1, 12, 2, 12));
		sheet.addMergedRegion(new Region(1, 13, 2, 13));
		sheet.addMergedRegion(new Region(1, 14, 2, 14));
		sheet.addMergedRegion(new Region(1, 15, 2, 15));
		sheet.addMergedRegion(new Region(1, 16, 2, 16));
		sheet.addMergedRegion(new Region(1, 17, 2, 17));
		sheet.addMergedRegion(new Region(1, 18, 2, 18));
		List list = (List) model.get("list");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		boolean i = false;

		for (int arg25 = 0; arg25 < list.size(); ++arg25) {
			Object[] os = (Object[]) list.get(arg25);
			String estName = os[8].toString();
			String orderDate = sf.format((Date) os[2]);
			String billCode = os[1].toString();
			BigDecimal cost = (BigDecimal) os[3];
			BigDecimal adjustCost = (BigDecimal) os[5];
			BigDecimal materialCost = (BigDecimal) os[9];
			BigDecimal cooperCost = (BigDecimal) os[10];
			BigDecimal feeCost = (BigDecimal) os[11];
			BigDecimal profitCost = (BigDecimal) os[12];
			BigDecimal collectedCost = (BigDecimal) os[4];
			BigDecimal remainCost = cost.subtract(collectedCost)
					.add(adjustCost);
			String closDate = "";
			if (os[22] != null) {
				closDate = sf.format((Date) os[22]);
			}

			r = sheet.createRow(arg25 + 3);
			r.createCell(0).setCellValue(new HSSFRichTextString(estName));
			r.createCell(1).setCellValue(new HSSFRichTextString(orderDate));
			r.createCell(2).setCellValue(new HSSFRichTextString(billCode));
			r.createCell(3).setCellValue(
					new HSSFRichTextString((String) os[15]));
			r.createCell(4).setCellValue(cost.doubleValue());
			r.createCell(5).setCellValue(adjustCost.doubleValue());
			r.createCell(6).setCellValue(materialCost.doubleValue());
			r.createCell(7).setCellValue(cooperCost.doubleValue());
			r.createCell(8).setCellValue(feeCost.doubleValue());
			r.createCell(9).setCellValue(profitCost.doubleValue());
			r.createCell(10).setCellValue(
					new HSSFRichTextString(os[20].toString() + "%"));
			r.createCell(11).setCellValue(collectedCost.doubleValue());
			r.createCell(12).setCellValue(remainCost.doubleValue());
			r.createCell(13).setCellValue(
					new HSSFRichTextString((String) os[6]));
			r.createCell(14).setCellValue(
					new HSSFRichTextString((String) os[16]));
			r.createCell(15).setCellValue(
					new HSSFRichTextString((String) os[18]));
			r.createCell(16).setCellValue(
					new HSSFRichTextString((String) os[19]));
			r.createCell(17).setCellValue(
					new HSSFRichTextString((String) os[17]));
			r.createCell(18).setCellValue(new HSSFRichTextString(closDate));
		}

		ServletOutputStream arg26 = response.getOutputStream();
		workbook.write(arg26);
		arg26.flush();
		arg26.close();
	}
}